from dvadmin.utils.serializers import CustomModelSerializer
from rest_framework import serializers

from .models import Database, DatabaseUser, Tnsname, DatabaseLink


class DatabaseSerializer(CustomModelSerializer):
    """数据库 序列化器"""

    class Meta:
        model = Database
        fields = "__all__"


class DatabaseCreateUpdateSerializer(CustomModelSerializer):
    """数据库 创建和更新序列化器"""

    class Meta:
        model = Database
        fields = "__all__"

class DatabaseImportSerializer(CustomModelSerializer):
    """数据库 导入序列化器"""
    
    class Meta:
        model = Database
        fields = "__all__"


class DatabaseExportSerializer(CustomModelSerializer):
    """数据库 导出序列化器"""
    
    class Meta:
        model = Database
        fields = "__all__"


class DatabaseUserSerializer(CustomModelSerializer):
    """数据库用户 序列化器"""

    class Meta:
        model = DatabaseUser
        fields = "__all__"

class DatabaseUserCreateUpdateSerializer(CustomModelSerializer):
    """数据库用户 创建和更新序列化器"""

    class Meta:
        model = DatabaseUser
        fields = "__all__"

class DatabaseUserImportSerializer(CustomModelSerializer):
    """数据库用户 导入序列化器"""

    def run_validation(self, data={}):
        if type(data) is dict:
            data['database'] = Database.objects.exclude(env='STB').get(name=data.get('database')).id
        return super().run_validation(data)
    
    class Meta:
        model = DatabaseUser
        fields = "__all__"


class DatabaseUserExportSerializer(CustomModelSerializer):
    """数据库用户 导出序列化器"""
    
    database = serializers.CharField(source="database.name", default= None, read_only=True, help_text='数据库')

    class Meta:
        model = DatabaseUser
        fields = "__all__"


class TnsnameSerializer(CustomModelSerializer):
    """TNSNAME 序列化器"""

    class Meta:
        model = Tnsname
        fields = "__all__"


class TnsnameCreateUpdateSerializer(CustomModelSerializer):
    """TNSNAME 创建和更新序列化器"""

    class Meta:
        model = Tnsname
        fields = "__all__"


class TnsnameImportSerializer(CustomModelSerializer):
    """TNSNAME 导入序列化器"""

    class Meta:
        model = Tnsname
        fields = "__all__"


class TnsnameExportSerializer(CustomModelSerializer):
    """TNSNAME 导出序列化器"""

    class Meta:
        model = Tnsname
        fields = "__all__"


class DatabaseLinkSerializer(CustomModelSerializer):
    """DBLink 序列化器"""

    class Meta:
        model = DatabaseLink
        fields = "__all__"


class DatabaseLinkCreateUpdateSerializer(CustomModelSerializer):
    """DBLink 创建和更新序列化器"""

    class Meta:
        model = DatabaseLink
        fields = "__all__"


class DatabaseLinkImportSerializer(
    CustomModelSerializer):
    """DBLink 导入序列化器"""

    def run_validation(self, data={}):
        if type(data) is dict:
            data['database'] = Database.objects.get(sid=data.get('database')).id
        return super().run_validation(data)

    class Meta:
        model = DatabaseLink
        fields = "__all__"


class DatabaseLinkExportSerializer(CustomModelSerializer):
    """DBLink 导出序列化器"""

    database = serializers.CharField(source="database.sid", default=None, read_only=True, help_text='数据库')

    class Meta:
        model = DatabaseLink
        fields = "__all__"

